home *** CD-ROM | disk | FTP | other *** search
/ Shareware Super Platinum 8 / Shareware Super Platinum 8.iso / mac / WIN_PRO / VIEWS.ZIP;1 / CVDZIP.EXE / MESSENGR.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-20  |  2.4 KB  |  124 lines

  1. /*
  2.     messengr.cpp
  3.  
  4.     Message retrieval class
  5.  
  6.     C++/Views 2.0 Demo
  7.     Copyright (c) 1992, by Liant Software Corp.
  8.     ALL RIGHTS RESERVED.
  9.  
  10.     Revision History:
  11.     -----------------
  12. */
  13.  
  14. #include "messengr.h"
  15. #include "file.h"
  16. #include "tagstrm.h"
  17. #include "dictinry.h"
  18.  
  19. Messenger::Messenger(char *filename)
  20. /*
  21.     Construct a Messenger object.
  22.  
  23.     A messager object provides a mechanism for retrieving text messages
  24.     from a file.
  25.  
  26.     Each message is preceded by an %item% label (delimited by '%' characters).
  27.  
  28.     You can retrieve the message associated with a particulay item
  29.     with Messenger::getMessage(char *item).
  30. */
  31. {
  32.     lastoffs = 0L;
  33.  
  34.     currMsg.puts("");
  35.  
  36.     msgFile = new VFile(filename);
  37.  
  38.     if (!msgFile->open(ReadOnly)) {
  39.         delete msgFile;
  40.         msgFile = 0;
  41.         tagStrm = 0;
  42.         idxDict = 0;
  43.     }
  44.     else {
  45.         tagStrm = new VTagStream();
  46.         tagStrm->beginScan(msgFile, NIL);
  47.         tagStrm->tags('%', '%');
  48.  
  49.         idxDict = new VDictionary();
  50.      }
  51. }
  52.  
  53. Messenger::~Messenger()
  54. {
  55.     if (msgFile) {
  56.         msgFile->close();
  57.         delete msgFile;
  58.         delete tagStrm;
  59.         delete idxDict;
  60.     }
  61. }
  62.  
  63. VStream &Messenger::getMessage(char *item)
  64. /*
  65.     Find message 'item' in the message file.
  66. */
  67. {
  68.     VString    index;
  69.     long    offset;
  70.  
  71.     currMsg.reset();    
  72.  
  73.     if (!msgFile) {
  74.         currMsg.puts("Unable to find message file.");
  75.         return(currMsg);
  76.     }
  77.  
  78.     /* set default error message */
  79.     currMsg.puts("Message item not found");    
  80.  
  81.     if (tagStrm) {
  82.         /* search for tag "item" */
  83.         tagStrm->beginScan(msgFile, NIL);
  84.  
  85.         /* first, see if we've already found the message */
  86.         if ((offset = (long) idxDict->getValueAtKey(&VString(item))) != 0L) {
  87.             tagStrm->fromAt(offset);
  88.  
  89.             /* found it: read in the message */
  90.             tagStrm->skipTo('\n');
  91.             tagStrm->scan(msgFile, &currMsg);
  92.             tagStrm->toAt(0L);
  93.             tagStrm->getTag(index);
  94.             tagStrm->ungetTo();
  95.             currMsg.putch('\0');
  96.         }
  97.         else {
  98.             /* search for the message (start from the end) */
  99.             tagStrm->fromAt(lastoffs);
  100.  
  101.             while(tagStrm->getTag(index)) {
  102.                 /* save the file offset of the message */
  103.                 offset = (long) tagStrm->fromAt();
  104.                 idxDict->atKeyPut(new VString(index.gets()), offset);
  105.                 lastoffs = offset;
  106.  
  107.                 if (index == item) {
  108.                     /* found the one we're looking for, read it in */
  109.                     tagStrm->skipTo('\n');
  110.                     tagStrm->scan(msgFile, &currMsg);
  111.                     tagStrm->toAt(0L);
  112.                     tagStrm->getTag(index);
  113.                     tagStrm->ungetTo();
  114.                     currMsg.putch('\0');
  115.                     break;
  116.                 }
  117.             }
  118.         }
  119.     }
  120.  
  121.     return(currMsg);
  122. }
  123.  
  124.